<?php
class BudgetService
{

    /*
    * 是否是合适的地板主材料
    *
    */
    public static function isVaildGroundMaterial ($type_Id)
    {
        if($type_Id == MsgConst::BUDGET_GROUNDTYPE_BOARD  || $type_Id == MsgConst::BUDGET_GROUNDTYPE_TILE)
            return;

        throw new Exception("非地板主材料id ".$type_Id);


    }

    /*
    * 是否是合适的墙面主材料
    *
    */
    public static function isVaildWallMaterial ($type_Id)
    {
        if($type_Id == MsgConst::BUDGET_WALLTYPE_TILE  || $type_Id == MsgConst::BUDGET_WALLTYPE_PAPER || $type_Id == MsgConst::BUDGET_WALLTYPE_PAINT)
            return;

        throw new Exception("非墙面主材料id ".$type_Id);


    }


    /*
    * 确定case 是否全部填写完毕
    *
    */
    public static function checkCaseStatus($case)
    {
        $inputinfo = $case->input_info;
        $input_array = json_decode($inputinfo);

        $caseid =$case->id;
        $num = 0;

        $flag = true;
        for($i=0;$i<count($input_array);$i++)
        {
            $type_id =  $input_array[$i]->type_id;
            $num = $input_array[$i]->num;

            for($t=0;$t<$num;$t++)
            {

                //查找该案例是否已经有数据
                $searchsql = Util::initSearchSql();

                $searchsql = Util::getSerarchStrAdd($searchsql,"case_id",$caseid);
                $searchsql = Util::getSerarchStrAdd($searchsql,"index_num",$t);
                $searchsql = Util::getSerarchStrAdd($searchsql,"house_type_id",$type_id);

                $obj = BudgetCaseDetail::model()->find($searchsql);

                if(empty($obj))
                {
                    $flag = false;
                    break;
                }

            }

            if($flag==false)
                break;

        }

        if($flag)
        {
            $case->status = MsgConst::BUDGET_STATUS_FINISHINPUT;
            Util::updateObjData($case);
            Util::saveObjData($case);
        }


    }


    /*
    * 获取case detail
    *
    */
    public static function getCaseDetail($case_id,$index,$type_id)
    {
         $obj  = BudgetCaseDetail::model()->find(" is_delete=0 and case_id=:case_id and index_num=:index_num and house_type_id=:house_type_id ",
                     array(":case_id" => $case_id,":index_num" => $index,":house_type_id" => $type_id));

        return $obj;




    }

    /*
    *
    *
    */
    public static function generateRestuleInfo($obj,$case_id)
    {


        $result_info = array();
        $count = 0;
        $inputinfo = $obj->input_info;

        $typearray = json_decode($inputinfo);


        $remark = "";

        for($i=0;$i<count($typearray);$i++)
        {
            $typeobj = $typearray[$i];
            $type_id = $typeobj->type_id;

            $type = BudgetHousetype::model()->findByPK($type_id);
            Util::checkExistAndNotDelete($type);

            $num = $typeobj->num;

            if($num>0)
                $remark = $remark.$num.$type->shortname;

            for($t=0;$t<$num;$t++)
            {
                $num_info = array();
                $num_info["count"]=$count;
                $num_info["status"]=MsgConst::BUDGET_STATUS_INIT;

                $num_info["type_id"] = $type_id;
                $num_info["type_name"] = $type->name;
                $num_info["index"] = $t;


                if($case_id>0)
                {
                    $detailobj = BudgetService::getCaseDetail($case_id,$t,$type_id);

                    if(empty($detailobj)==false)
                    $num_info["detailinfo"] = ($detailobj);

                }
                else
                {
                    $num_info["detailinfo"] = "";

                }







                $count++;



                array_push($result_info,$num_info);
            }





        }

        $obj->input_desc = $remark;
       //  $obj->result_info =  json_encode($result_info);
        $obj->result_info =  $result_info;
        return $obj;


    }

    /*
    *
    *
    */
    public static function getCaseIdByUserid($user_id)
    {
        $searchsql = Util::initSearchSql();
        $searchsql = Util::getSerarchStrAdd($searchsql,"user_id",$user_id);

        //排序
        $searchsql = $searchsql . " order by create_time  desc";

         $obj  = BudgetCase::model()->find($searchsql);

        if(empty($obj)==false)
            return $obj->id;

        return -1;


    }


    /*
    * 获取材料列表
    *
    */
    public static function getMaterialList ($case_info,$case_detail_list,$showlogfalg)
    {
        //获取大分类列表
        $m_type_list = BudgetService::getFirstMaterialTypeList();


        $return_array = array();






        for($i=0;$i<count($m_type_list);$i++)
        {
            $m_type = $m_type_list[$i];

            //获取小分类列表
            $s_type_list = BudgetService::getSecondMaterialTypeList($m_type->id);

            $s_count = count($s_type_list);

            //遍历子列表
            for($t=0;$t<$s_count;$t++)
            {
                $s_type = $s_type_list[$t];

                //创建种类数组
                 $s_type_array = array();

                $s_type_array["id"]=$s_type->id;
                $s_type_array["name"]=$m_type->name."-".$s_type->name;


                //获取材料列表
                $m_list = BudgetService::getMaterialListByTypeID($s_type->id);



                $return_m_list = array();


                //遍历每个材料

                for($m=0;$m<count($m_list);$m++)
                {
                    $material = $m_list[$m];
                    $material->main_type_id = $m_type->id;


//                    Util::echolog("12231");

                    if($showlogfalg)
                    Util::echolog($material->name);

                    $material->num = BudgetService::getMaterialNum($case_info,$m_list[$m],$case_detail_list,$showlogfalg);
                    if($material->num > 0)
                        array_push($return_m_list,$material);


                    if($showlogfalg)
                    Util::echolog("-----------------------");

                }
                $s_type_array["mlist"] = $return_m_list;

                //加入数据

                array_push($return_array,$s_type_array);




            }


            //子列表为空
            if($s_count == 0)
            {


                if($showlogfalg)
                    Util::echolog("子列表为空");

                //创建种类数组
                $s_type_array = array();

                $s_type_array["id"]=$m_type->id;
                $s_type_array["name"]=$m_type->name;


                //获取材料列表
                $m_list = BudgetService::getMaterialListByTypeID($m_type->id);



                $return_m_list = array();


                //遍历每个材料

                for($m=0;$m<count($m_list);$m++)
                {
                    $material = $m_list[$m];
                    $material->main_type_id = $m_type->id;


//                    Util::echolog("12231");

                    if($showlogfalg)
                        Util::echolog($material->name);

                    $material->num = BudgetService::getMaterialNum($case_info,$m_list[$m],$case_detail_list,$showlogfalg);
                    if($material->num > 0)
                        array_push($return_m_list,$material);


                    if($showlogfalg)
                        Util::echolog("-----------------------");

                }
                $s_type_array["mlist"] = $return_m_list;

                //加入数据

                array_push($return_array,$s_type_array);

            }



        }

        return $return_array;


    }

    /*
    * 获取一级材料种类列表
    *
    */
    public static function getFirstMaterialTypeList()
    {
        $sql = Util::initSearchSql();
        $sql= Util::getSerarchStrAdd($sql,"level",1);
        $sql = $sql . " order by id  asc";



        $list = BudgetMaterialType::model()->findAll($sql);


        return $list;





    }

    /*
   * 获取二级材料种类列表
   *
   */
    public static function getSecondMaterialTypeList($id)
    {
        $sql = Util::initSearchSql();
        $sql= Util::getSerarchStrAdd($sql,"level",2);
        $sql= Util::getSerarchStrAdd($sql,"parent_id",$id);
        $sql = $sql . " order by id  asc";


        $list = BudgetMaterialType::model()->findAll($sql);

        return $list;





    }

    /*
    *
    * 获取材料列表
    */
    public static function getMaterialListByTypeID($type_id)
    {

        $sql = Util::initSearchSql();
       // $sql= Util::getSerarchStrAdd($sql,"level",2);
        $sql= Util::getSerarchStrAdd($sql,"type_id",$type_id);
        $sql = $sql . " order by id  asc";


        $list = BudgetMaterial::model()->findAll($sql);

        return $list;



    }


    /*
    * 获取材料数量
    *
    */
    public static function getMaterialNum($case_info,$material,$case_detail_list,$showlogfalg)
    {


        //主材


        if($material->main_type_id == MsgConst::BUDGET_TYPE_MAIN_ID)
        {

            if($showlogfalg)
            Util::echolog("主材 ".$material->main_type_id);
            return BudgetService::getMainTypeNum($case_info,$material,$case_detail_list,$showlogfalg);

        }

        //辅材

        if($material->main_type_id == MsgConst::BUDGET_TYPE_ASSIST_ID ||  $material->main_type_id == MsgConst::BUDGET_TYPE_INSTALL_ID)
        {

            if($showlogfalg)
                Util::echolog("辅材 ".$material->main_type_id);

            //公式
            $formula = urldecode($material->formula);

            if($showlogfalg)
            Util::echolog("公式:".$formula);

            $num = 0;

            //特殊材料特殊处理

            if(BudgetService::isSpecialMaterial($material)==true)
            {
                if($showlogfalg)
                    Util::echolog("特殊材料");
                $num = BudgetService::getSpecialNumMaterial($material,$case_detail_list,$showlogfalg);
                return $num;
            }

            //公式 只是数字
            if(is_numeric($formula)==true)
            {
                $num = (int)$formula;

                return $num;

            }

            else
            {
                $formula = BudgetService::getRightFormula($formula,$case_detail_list,$showlogfalg);

                if($showlogfalg)
                Util::echolog("整理后缀公式:".$formula);

                $newformual = BudgetService::getNewFormula($case_info,$material,$formula);


                if($showlogfalg)
                Util::echolog("后缀公式:".$newformual);

//                if(is_numeric($newformual)==true)
//                    return  (int)$newformual;

                //计算

                $newStrList = BudgetFormulaService::getPostExp($newformual);

                if($showlogfalg)
                Util::echolog("最终后缀:".$newStrList);


                $num =  BudgetFormulaService::getValueByPostExp($newStrList);


                if($showlogfalg)
                Util::echolog("最终结果:".$num);

                $ratio = $material->ratio;

                if($showlogfalg)
                    Util::echolog("比例:".$ratio);

                if($ratio != 1)
                {
                    $num =  ceil((float)$num * $ratio);

                    if($showlogfalg)
                        Util::echolog("结果:".$num);
                }


                return $num;

            }




        }








    }


    /*
    * 获取新的公式
    *
    */
    public static function getNewFormula($cast_info,$material,$formula)
    {
        $newstr = explode(' ', $formula);

        $returnarray = array();

        for($i=0;$i<count($newstr);$i++)
        {
            $str = $newstr[$i];

            //数字
           if(is_numeric($str))
           {
               array_push($returnarray,$str);

               continue;

           }


            //操作符
            if(strstr($str,"+") || strstr($str,"-") || strstr($str,"*") || strstr($str,"/") || strstr($str,"(") || strstr($str,")") )
            {

                array_push($returnarray,$str);
                continue;

            }


            //特殊符号
            $num =  BudgetService::getCalcMaterialNum($cast_info,$material,$str);
            array_push($returnarray,$num);




        }


        $returnstr = "";

        for($i=0;$i<count($returnarray);$i++)
        {
            if($i==0)
                $returnstr = $returnarray[$i];
            else
                $returnstr =  $returnstr." ".$returnarray[$i];
        }

        return $returnstr;

    }


    /*
    *  calc_num
    *
    */
    public static function getCalcMaterialNum($cast_info,$material,$string)
    {

        //

        return 10;




    }


    /*
    *
      获取主材显示数量
    *
    */
    public static function getMainTypeNum($case_info,$material,$case_detail_list,$showlogfalg)
    {

        //地面

        if($material->type_id == MsgConst::BUDGET_TYPE_SUB_GROUND_TYPE )
        {
            if($showlogfalg)
            Util::echolog("地面  ".$material->type_id);
            return BudgetService::getMainGroundNum($case_info,$material,$case_detail_list,$showlogfalg);
        }



        //墙面
        if($material->type_id ==   MsgConst::BUDGET_TYPE_SUB_WALL_ID )
        {
            if($showlogfalg)
            Util::echolog("墙面  ".$material->type_id);
            return BudgetService::getMainWallNum($case_info,$material,$case_detail_list,$showlogfalg);

        }


        return 10;


    }


    /*
    * 获取地面材料
    *
    */
    public static function getMainGroundNum($case_info,$material,$case_detail_list,$showlogfalg)
    {

        $num = 0;

        $l = count($case_detail_list);

        for($i=0;$i<$l;$i++)
        {
            $case_detail = $case_detail_list[$i];

            $name = $case_detail->house_type;
            $index = $case_detail->index_num;
            $length = $case_detail->length;
            $width=$case_detail->width;
            $height =$case_detail->height;

            $ground_type_id = $case_detail->ground_type_id;
            $wall_type_id = $case_detail->wall_type_id;

            //Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->ground_type_name."  ".$case_detail->wall_type_name);


            //地砖

            if($material->name == "地砖")
            {
                if($ground_type_id == MsgConst::BUDGET_GROUNDTYPE_TILE)
                {



                    $square = (float)$width * (float)$length;

                    if($showlogfalg)
                    Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->ground_type_name."  ".$square);

                    $num += $square;


                }

            }

            //地板

            if($material->name == "地板")
            {
                if($ground_type_id == MsgConst::BUDGET_GROUNDTYPE_BOARD)
                {



                    $square = (float)$width * (float)$length;

                    if($showlogfalg)
                    Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->ground_type_name."  ".$square);

                    $num += $square;


                }

            }





        }







        return $num;


    }



    /*
    * 获取墙面材料
    *
    */
    public static function getMainWallNum($case_info,$material,$case_detail_list,$showlogfalg)
    {

        $num = 0;

        $l = count($case_detail_list);

        for($i=0;$i<$l;$i++)
        {
            $case_detail = $case_detail_list[$i];

            $name = $case_detail->house_type;
            $index = $case_detail->index_num;
            $length = $case_detail->length;
            $width=$case_detail->width;
            $height =$case_detail->height;

            $ground_type_id = $case_detail->ground_type_id;
            $wall_type_id = $case_detail->wall_type_id;

            //Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->ground_type_name."  ".$case_detail->wall_type_name);


            //墙砖

            if($material->name == "墙砖")
            {
                if($wall_type_id == MsgConst::BUDGET_WALLTYPE_TILE)
                {



                    $square = ((float)$width + (float)$length)*2*(float)$height;

                    if($showlogfalg)
                    Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->wall_type_name."  ".$square);

                    $num += $square;


                }

            }

            //墙纸

            if($material->name == "墙纸")
            {
                if($wall_type_id == MsgConst::BUDGET_WALLTYPE_PAPER)
                {



                    $square = ((float)$width + (float)$length)*2*(float)$height;

                    if($showlogfalg)
                    Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->wall_type_name."  ".$square);

                    $num += $square;


                }

            }

            //墙纸

            if($material->name == "乳胶漆")
            {
                if($wall_type_id == MsgConst::BUDGET_WALLTYPE_PAINT)
                {



                    $square = ((float)$width + (float)$length)*2*(float)$height;

                    //客厅 阳台
                    if($case_detail->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_LIVING_ROOM ||  $case_detail->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_BALCONY ||  $case_detail->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_BED_ROOM)
                        $square = ((float)$width + (float)$length)*2*(float)$height+(float)$length*(float)$width;


                    if($showlogfalg)
                    Util::echolog($name."-".$index." 长＊宽＊高 : ".$length."*".$width."*".$height."  ".$case_detail->wall_type_name."  ".$square);

                    $num += $square;


                }

            }





        }







        return $num;


    }


    /*
    * 获取案例详情列表
    *
    */
    public static function getCaseDetailList($case_id,$case_info)
    {
        $return_array = array();
        $numarray = array();

         $detaillist  = BudgetCaseDetail::model()->findAll("case_id=:case_id ",
                     array(":case_id" => $case_id));

        $input_info = $case_info->input_info;

        $array = json_decode($input_info);

        //print_r($array);

        for($i=0;$i<count($array);$i++)
        {
            $type_id = $array[$i]->type_id;
            $num = $array[$i]->num;

            $numarray[$type_id] = $num;

        }

        for($i=0;$i<count($detaillist);$i++)
        {
            $detail_info = $detaillist[$i];

            $typeid =  $detail_info->house_type_id;

            $indexnum =  $detail_info->index_num;

            $maxnum =   $numarray[$typeid];

            if($indexnum < $maxnum)
                array_push($return_array,$detail_info);
        }

        return $return_array;




    }

    /*
    * 获取平方信息
    *
    */
    public static function getSquareByTypeID($worker_type,$case_detaillist)
    {
        $squre = 0;
        //设计师
        // if($worker_type->name == "设计师")
        {
            $l = count($case_detaillist);

            for($i=0;$i<$l;$i++)
            {
                $case_info = $case_detaillist[$i];
                $s = (float)$case_info->length *(float)$case_info->width;


                $squre=$squre+$s;
            }

            return $squre;
        }

        //


        return 50;



    }

    /*
    *  替换表达式中数值
    *
    */
    public static function getRightFormula($formula,$case_detail_list,$showlogfalg)
    {
        $array = explode(" ",$formula);

        $newformual = "";

        for($i=0;$i<count($array);$i++)
        {
            $obj = $array[$i];

            if(is_numeric($obj))
            {
                if($showlogfalg)
                Util::echolog("数字 ".$obj);

            }

            else if($obj == "+" || $obj == "-" || $obj == "*" || $obj == "/")
            {
                if($showlogfalg)
                Util::echolog("运算符 ".$obj);
            }

            else {

                if($showlogfalg)
                Util::echolog("固定公式 ".$obj);

                $value = BudgetService::getValueOfExpression($obj,$case_detail_list);

                if($showlogfalg)
                Util::echolog("转换结果 ".$value);

                $array[$i] = $value;


            }




        }

        for($i=0;$i<count($array);$i++)
        {
            $obj = $array[$i];

            if($i==0)
                $newformual = $obj;
            else
                $newformual = $newformual." ".$obj;

        }

        return $newformual;


    }

    /*
    *  换取对应的数值
    *
    */
    public static function getValueOfExpression ($express,$case_detail_list)
    {
        //总房间数量 算上所有面积
        if($express == "E1")
        {
            $num =count($case_detail_list);

//            for($i=0;$i<count($case_detail_list);$i++)
//            {
//                $obj = $case_detail_list[$i];
//
//                if($obj->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_BED_ROOM)
//                    $num++;
//            }
            return $num;

        }

        //所有房间面积总和
        if($express == "S1")
        {
            $square = 0;

            for($i=0;$i<count($case_detail_list);$i++)
            {
                $obj = $case_detail_list[$i];
                $s = $obj->length * $obj->width;

                $square +=$s;
            }

            return $square;
        }

        //卫生间数量
        if($express == "C1")
        {

            $num =0;

            for($i=0;$i<count($case_detail_list);$i++)
            {
                $obj = $case_detail_list[$i];

                if($obj->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_TOLIET)
                    $num++;
            }
            return $num;


        }


        //阳台数量
        if($express == "F1")
        {

            $num =0;

            for($i=0;$i<count($case_detail_list);$i++)
            {
                $obj = $case_detail_list[$i];

                if($obj->house_type_id == MsgConst::BUDGET_HOUSE_TYPE_BALCONY)
                    $num++;
            }
            return $num;


        }



        return 0;


    }

    /*
    *
    * 是否为特殊材料
    */
    public static function isSpecialMaterial($material)
    {
        if($material->name == "地砖踢脚线" || $material->name =="地板踢脚线" || $material->name == "地板龙骨" || $material->name == "墙纸基膜" || $material->name == "墙纸胶水")
        {
            return true;
        }

        return false;


    }

    /*
    *  获取
    *
    */
    public static function getSpecialNumMaterial($material,$case_detail_list,$showlogfalg)
    {
        $num = 0;

        $l = count($case_detail_list);

        if($showlogfalg)
            Util::echolog($material->name.$l);
        //地砖踢脚线
        if($material->name == "地砖踢脚线") {

            for ($i = 0; $i < $l; $i++) {
                $case_detail = $case_detail_list[$i];

                $name = $case_detail->house_type;
                $index = $case_detail->index_num;
                $length = $case_detail->length;
                $width = $case_detail->width;
                $height = $case_detail->height;

                $ground_type_id = $case_detail->ground_type_id;
                $wall_type_id = $case_detail->wall_type_id;


                //地砖
                if ($ground_type_id == MsgConst::BUDGET_GROUNDTYPE_TILE) {


                    $square = ((float)$width + (float)$length) * 2;

                    if ($showlogfalg)
                        Util::echolog($name . "-" . $index . " 长＊宽＊高 : " . $length . "*" . $width . "*" . $height . "  " . $case_detail->wall_type_name . "  " . $square);

                    $num += $square;


                }


            }



        }

        //地板踢脚线
        if($material->name == "地板踢脚线") {

            for ($i = 0; $i < $l; $i++) {
                $case_detail = $case_detail_list[$i];

                $name = $case_detail->house_type;
                $index = $case_detail->index_num;
                $length = $case_detail->length;
                $width = $case_detail->width;
                $height = $case_detail->height;

                $ground_type_id = $case_detail->ground_type_id;
                $wall_type_id = $case_detail->wall_type_id;


                //地板
                if ($ground_type_id == MsgConst::BUDGET_GROUNDTYPE_BOARD) {


                    $square = ((float)$width + (float)$length) * 2;

                    if ($showlogfalg)
                        Util::echolog($name . "-" . $index . " 长＊宽＊高 : " . $length . "*" . $width . "*" . $height . "  " . $case_detail->wall_type_name . "  " . $square);

                    $num += $square;


                }


            }



        }


        //地板龙骨

        if($material->name == "地板龙骨") {

            for ($i = 0; $i < $l; $i++) {
                $case_detail = $case_detail_list[$i];

                $name = $case_detail->house_type;
                $index = $case_detail->index_num;
                $length = $case_detail->length;
                $width = $case_detail->width;
                $height = $case_detail->height;

                $ground_type_id = $case_detail->ground_type_id;
                $wall_type_id = $case_detail->wall_type_id;


                //地板
                if ($ground_type_id == MsgConst::BUDGET_GROUNDTYPE_BOARD) {


                    $square = (float)$width * (float)$length ;

                    if ($showlogfalg)
                        Util::echolog($name . "-" . $index . " 长＊宽＊高 : " . $length . "*" . $width . "*" . $height . "  " . $case_detail->wall_type_name . "  " . $square);

                    $num += $square;


                }


            }



        }


        //墙纸基膜
        //墙纸胶水

        if($material->name == "墙纸基膜" || $material->name == "墙纸胶水" ) {

            for ($i = 0; $i < $l; $i++) {
                $case_detail = $case_detail_list[$i];

                $name = $case_detail->house_type;
                $index = $case_detail->index_num;
                $length = $case_detail->length;
                $width = $case_detail->width;
                $height = $case_detail->height;

                $ground_type_id = $case_detail->ground_type_id;
                $wall_type_id = $case_detail->wall_type_id;


                //墙纸
                if ($wall_type_id == MsgConst::BUDGET_WALLTYPE_PAPER) {


                    $square = ((float)$width + (float)$length)*2*(float)$height;

                    if ($showlogfalg)
                        Util::echolog($name . "-" . $index . " 长＊宽＊高 : " . $length . "*" . $width . "*" . $height . "  " . $case_detail->wall_type_name . "  " . $square);

                    $num += $square;


                }


            }



        }





        //比例
        //$num = $num *$material->ratio;

        if($showlogfalg)
            Util::echolog("总数 ".$num);

        $num =  ceil((float)$num * $material->ratio);

        if($showlogfalg)
            Util::echolog("最终总数 ".$num);

        return $num;


    }


}


?>